package com.itheima.day08_api;

/**
 * 目标：完成随机产生验证码，验证码的每位可能是数字，大写字母，小写字母
 */
public class Demo4_String {
    public static void main(String[] args) {
        System.out.println("getCode(4) = " + getCode(4));
        System.out.println("getCode(5) = " + getCode(5));
        System.out.println("--------------");
        System.out.println("getCode1(5) = " + getCode1(5));
        System.out.println("getCode1(4) = " + getCode1(4));
    }

    /**
     * 1.设计一个方法返回指定位数的验证码
     */
    public static String getCode(int n) {
        String code = "";
        for (int i = 0; i < n; i++) {
            int number = (int) (Math.random() * 10);
            char ch = (char) (Math.random() * 26 + 'a');
            char ch1 = (char) (Math.random() * 26 + 'A');
            int index = (int) (Math.random() * 3);
            switch (index) {
                case 0:
                    code += number;
                    break;
                case 1:
                    code += ch;
                    break;
                case 2:
                    code += ch1;
                    break;
            }
        }
        return code;
    }
    public static String getCode1(int n){
        String code = "";
        String data = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        for (int i = 0; i < n; i++) {
            int index = (int) (Math.random() * data.length());
            code += data.charAt(index);
        }
        return code;
    }
}
